package com.sys.common.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.users.pojo.AAdmins;
import com.sys.users.pojo.AAdminsEnum;
import com.sys.users.pojo.AUsers;
import com.sys.users.pojo.AUsersEnum;
import com.sys.users.service.IUsersDbService;

/**
 * ~Servlet开发分三步走: 写一个java代码,Servlet 配置web.xml 在浏览器中访问
 * 
 * ~初始化,Service,一个销毁
 * 
 * @author wangshMac
 */
@Controller
@RequestMapping("/") 
public class NoLoginController extends BaseController
{
	@Autowired
	private IUsersDbService usersDbService;
	
	
	/**
	 * 注册提交操作
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/registerSubmit")
	private String registerSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		ConstatFinalUtil.SYS_LOGGER.info("====registerSubmit===");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 存储多个结果,Map存储 */
		Map<String, Object> dataMapJava = new HashMap<String, Object>();
		apiResponse.setDataMapJava(dataMapJava);
		
		HttpSession session = request.getSession() ; 
		
		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String rand = request.getParameter("rand");
		
		/* session中获取验证码 */
		String randSess = (String) session.getAttribute("randSess");
		
		if(this.regexUtil.verifyCode(randSess, rand))
		{
			AUsers users = new AUsers();
			
			users.setEmail(email);
			users.setPassword(password);
			
			users.setStatus(AUsersEnum.STATUS_ENABLE.getStatus());
			
			ApiResponse<Object> apiResponseDb = this.usersDbService.saveOneUsersService(users);
			
			/* 设置数据库的保存信息 */
			apiResponse.setCode(apiResponseDb.getCode());
		}else
		{
			/* 验证码不正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
		}
		
		apiResponse.setInfo(
				ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		
		request.setAttribute("response", apiResponse.toJSON());
		return this.register(request, response);
	}

	/**
	 * 打开注册页面
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/register")
	private String register(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		ConstatFinalUtil.SYS_LOGGER.info("====register===");
		
		return "/head/register" ;
	}

	/**
	 * 登陆提交操作
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/loginSubmit")
	private String loginSubmit(HttpServletRequest request, HttpServletResponse response , HttpSession session) throws IOException, ServletException
	{
		ConstatFinalUtil.SYS_LOGGER.info("====loginSubmit===");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 存储多个结果,Map存储 */
		Map<String, Object> dataMapJava = new HashMap<String, Object>();
		apiResponse.setDataMapJava(dataMapJava);
		
		
		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String rand = request.getParameter("rand");
		
		/* session中获取验证码 */
		String randSess = (String) session.getAttribute("randSess");
		
		if(this.regexUtil.verifyCode(randSess, rand))
		{
			/* 验证码正确 */
			/* 查询db,处理业务逻辑 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			ApiResponse<AUsers> apiRes = this.usersDbService.findOneUsersService(condMap);
			AUsers users = apiRes.getDataOneJava();
			/* 检验邮箱 */
			if (users != null)
			{
				/* 将邮箱放到request中 */
				dataMapJava.put("email", users.getEmail());
				/* 检验密码 */
				if (this.encryptUtil.checkStr(password, users.getPassword()))
				{
					/* 状态必须为启用 */
					if (users.getStatus() == AAdminsEnum.STATUS_ENABLE.getStatus())
					{
						session.setAttribute("users", users);
						session.setAttribute("lastLoginTime", users.getLastLoginTime());
						
						/* 登陆成功 */
						users.setLastLoginTime(new Date());

						this.usersDbService.updateOneUsersService(users);
						
						/* 说明已经登陆 */
						return "redirect:/head/users/main" ;
					} else
					{
						/* 状态为禁用 */
						apiResponse.setCode(ApiResponseEnum.INFO_ACCOUNT_DISABLED.getStatus());
					}
				} else
				{
					apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
				}
			}else
			{
				/* 邮箱不存在 */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
			}
		}else
		{
			/* 验证码不正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
		}
		
		apiResponse.setInfo(
				ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		
		this.updateFlag = "true" ; 
		request.setAttribute("response", apiResponse.toJSON());
		return this.login(session);
	}

	/**
	 * 打开登陆页面
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/login")
	private String login(HttpSession session) throws IOException, ServletException
	{
		ConstatFinalUtil.SYS_LOGGER.info("=====login=======");
		/* 从session获取信息 */
		AUsers users = (AUsers) session.getAttribute("users");
		if(users != null)
		{
			/* 说明已经登陆 */
			return  "redirect:/head/users/main" ;
		}
		/* 页面跳转 */
		return "/head/login" ;
	}

	/**
	 * 提交管理员登陆
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/adminsLoginSubmit")
	private String adminsLoginSubmit(HttpServletRequest request, HttpServletResponse response , HttpSession session) throws IOException, ServletException
	{
		ConstatFinalUtil.SYS_LOGGER.info("===adminsLoginSubmit=====");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 存储多个结果,Map存储 */
		Map<String, Object> dataMapJava = new HashMap<String, Object>();
		apiResponse.setDataMapJava(dataMapJava);
		
		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String rand = request.getParameter("rand");
		
		/* session中获取验证码 */
		String randSess = (String) session.getAttribute("randSess");
		
		if(this.regexUtil.verifyCode(randSess, rand))
		{
			/* 验证码正确 */
			/* 查询db,处理业务逻辑 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			ApiResponse<AAdmins> apiRes = this.usersDbService.findOneAdminsService(condMap);
			AAdmins admins = apiRes.getDataOneJava();
			/* 检验邮箱 */
			if (admins != null)
			{
				/* 将邮箱放到request中 */
				dataMapJava.put("email", admins.getEmail());
				/* 检验密码 */
				if (this.encryptUtil.checkStr(password, admins.getPassword()))
				{
					/* 状态必须为启用 */
					if (admins.getStatus() == AAdminsEnum.STATUS_ENABLE.getStatus())
					{
						session.setAttribute("admins", admins);
						session.setAttribute("lastLoginTime", admins.getLastLoginTime());
						
						/* 登陆成功 */
						admins.setLastLoginTime(new Date());

						this.usersDbService.updateOneAdminsService(admins);
						
						return  "redirect:/back/admins/main" ;
						
					} else
					{
						/* 状态为禁用 */
						apiResponse.setCode(ApiResponseEnum.INFO_ACCOUNT_DISABLED.getStatus());
					}
				} else
				{
					apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
				}
			}else
			{
				/* 邮箱不存在 */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
			}
		}else
		{
			/* 验证码不正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
		}
		
		apiResponse.setInfo(
				ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		
		request.setAttribute("response", apiResponse.toJSON());
		return this.adminsLogin(session);
	}

	/**
	 * 打开管理员登陆页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/adminsLogin")
	private String adminsLogin(HttpSession session) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("===adminsLogin=====");
		/* 从session获取信息 */
		AAdmins admins = (AAdmins) session.getAttribute("admins");
		if(admins != null)
		{
			/* 说明已经登陆 */
			return  "redirect:/back/admins/main" ;
		}
		/* 页面跳转 */
		return "/back/login" ;
	}
}
